package com.b1502.store2.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.GridView;
import android.widget.TextView;

import com.b1502.store2.R;
import com.b1502.store2.adapter.OrderGridviewadapter;
import com.b1502.store2.model.OrderList;
import com.b1502.store2.network.StoreParams;
import com.b1502.store2.util.UrlUtil;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.x;

import java.util.List;


public class MyOrderListActivity extends BaseActivity implements View.OnClickListener {

    private TextView mOrderlist_more;
    private TextView mOrderlist_xiaoliang;
    private TextView mOrderlist_money;
    private GridView mOrderlist_gridview;
    private OrderGridviewadapter mOrderGridviewadapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_order_list);
        Intent intent = getIntent();
        String name = intent.getStringExtra("name");
        String id = intent.getStringExtra("id");
        initview();
        initStoreBar(name);

        mOrderGridviewadapter = new OrderGridviewadapter(this);
        getCategory("categoryId", id);
        getProducts(id, 0);
        
        
        
    }

    private void initview() {
        mOrderlist_more = (TextView) findViewById(R.id.orderlist_more);
        mOrderlist_xiaoliang = (TextView) findViewById(R.id.orderlist_xiaoliang);
        mOrderlist_money = (TextView) findViewById(R.id.orderlist_money);
        mOrderlist_gridview = (GridView) findViewById(R.id.Orderlist_gridview);
        

        mOrderlist_more.setOnClickListener(this);
        mOrderlist_money.setOnClickListener(this);
        mOrderlist_xiaoliang.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.orderlist_more:
                mOrderlist_more.setTextColor(Color.RED);
                mOrderlist_xiaoliang.setTextColor(Color.GRAY);
                mOrderlist_money.setTextColor(Color.GRAY);
                break;
            case R.id.orderlist_xiaoliang:
                mOrderlist_more.setTextColor(Color.GRAY);
                mOrderlist_xiaoliang.setTextColor(Color.RED);
                mOrderlist_money.setTextColor(Color.GRAY);
                break;
            case R.id.orderlist_money:
                mOrderlist_more.setTextColor(Color.GRAY);
                mOrderlist_xiaoliang.setTextColor(Color.GRAY);
                mOrderlist_money.setTextColor(Color.RED);
                break;

        }
    }

    /**
     * @param param
     * @param categoryId
     */
    public void getCategory(String param, String categoryId) {
        StoreParams params = new StoreParams(UrlUtil.GetCategory, param, categoryId);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onSuccess(String result) {
                Log.i("MainActivity", result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    //分类界面

    public void getProducts(final String productCategoryId, int startRowIndex) {
        StoreParams params = new StoreParams(UrlUtil.GetProducts, productCategoryId, startRowIndex);
        x.http().get(params, new Callback.CommonCallback<String>() {
            

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onSuccess(String result) {
                Log.i("MainActivity", result);
                Gson gson=new Gson();
                OrderList orderList = gson.fromJson(result, OrderList.class);
                List<OrderList.ProductsBean> products = orderList.getProducts();
                mOrderGridviewadapter.setData(products);
                mOrderlist_gridview.setAdapter(mOrderGridviewadapter);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onFinished() {

            }
        });
    }



}